package com.ruoyi.report_yj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.syrw.annotation.FIllBasicInfoForTaiZhang;
import com.ruoyi.report_tg.domain.TgsybgBaDetail;
import com.ruoyi.report_tg.domain.vo.TgsybgBaDetailVo;
import com.ruoyi.report_tg.mapper.TgsybgBaDetailMapper;
import com.ruoyi.sycs.service.IBiQualificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_yj.domain.bo.SnjjqdsybgBsInfoBo;
import com.ruoyi.report_yj.domain.vo.SnjjqdsybgBsInfoVo;
import com.ruoyi.report_yj.domain.SnjjqdsybgBsInfo;
import com.ruoyi.report_yj.mapper.SnjjqdsybgBsInfoMapper;
import com.ruoyi.report_yj.service.ISnjjqdsybgBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 水泥净浆强度试验报告Service业务层处理
 *
 * @author llb
 * @date 2024-06-12
 */
@RequiredArgsConstructor
@Service
public class SnjjqdsybgBsInfoServiceImpl implements ISnjjqdsybgBsInfoService {

    private final SnjjqdsybgBsInfoMapper baseMapper;
    private final TgsybgBaDetailMapper detailMapper;

    private final IBiQualificationService biQualificationService;

    private static final Long[] keys = {168L, 169L, 170L, 171L, 172L, 173L};

    /**
     * 查询水泥净浆强度试验报告
     */
    @Override
    @FIllBasicInfo
    public SnjjqdsybgBsInfoVo queryById(Long excelId){
        SnjjqdsybgBsInfoVo snjjqdsybgBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<TgsybgBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<TgsybgBaDetail>()
            .eq(TgsybgBaDetail::getExcelId, excelId));
        baDetailVos.forEach(v->{
            v.setQualInfo(biQualificationService.queryById(v.getQualId()));
        });
        // 格式化 技术指标
        biQualificationService.compute(baDetailVos);
        // 转换成map形式
        Map<Long, TgsybgBaDetailVo> collect = baDetailVos.stream().collect(Collectors.toMap(TgsybgBaDetailVo::getQualId, v -> {
            return v;
        }));
        snjjqdsybgBsInfoVo.setBaDetailVos(collect);
        return snjjqdsybgBsInfoVo;
    }

    @Override
    @FIllBasicInfoForTaiZhang
    public SnjjqdsybgBsInfoVo queryByIdForTaizhang(Long excelId) {
        SnjjqdsybgBsInfoVo snjjqdsybgBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<TgsybgBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<TgsybgBaDetail>()
            .eq(TgsybgBaDetail::getExcelId, excelId));
        baDetailVos.forEach(v->{
            v.setQualInfo(biQualificationService.queryById(v.getQualId()));
        });
        // 格式化 技术指标
        biQualificationService.compute(baDetailVos);
        // 转换成map形式
        Map<Long, TgsybgBaDetailVo> collect = baDetailVos.stream().collect(Collectors.toMap(TgsybgBaDetailVo::getQualId, v -> {
            return v;
        }));
        snjjqdsybgBsInfoVo.setBaDetailVos(collect);
        return snjjqdsybgBsInfoVo;
    }

    /**
     * 查询水泥净浆强度试验报告列表
     */
    @Override
    public TableDataInfo<SnjjqdsybgBsInfoVo> queryPageList(SnjjqdsybgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SnjjqdsybgBsInfo> lqw = buildQueryWrapper(bo);
        Page<SnjjqdsybgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水泥净浆强度试验报告列表
     */
    @Override
    public List<SnjjqdsybgBsInfoVo> queryList(SnjjqdsybgBsInfoBo bo) {
        LambdaQueryWrapper<SnjjqdsybgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SnjjqdsybgBsInfo> buildQueryWrapper(SnjjqdsybgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SnjjqdsybgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, SnjjqdsybgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, SnjjqdsybgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), SnjjqdsybgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), SnjjqdsybgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), SnjjqdsybgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), SnjjqdsybgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), SnjjqdsybgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), SnjjqdsybgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), SnjjqdsybgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), SnjjqdsybgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), SnjjqdsybgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), SnjjqdsybgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), SnjjqdsybgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), SnjjqdsybgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), SnjjqdsybgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShejqd()), SnjjqdsybgBsInfo::getShejqd, bo.getShejqd());
        lqw.eq(StringUtils.isNotBlank(bo.getShijcc()), SnjjqdsybgBsInfo::getShijcc, bo.getShijcc());
        lqw.eq(StringUtils.isNotBlank(bo.getLiudd()), SnjjqdsybgBsInfo::getLiudd, bo.getLiudd());
        lqw.eq(StringUtils.isNotBlank(bo.getQvybw1()), SnjjqdsybgBsInfo::getQvybw1, bo.getQvybw1());
        lqw.eq(StringUtils.isNotBlank(bo.getZijrq1()), SnjjqdsybgBsInfo::getZijrq1, bo.getZijrq1());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyrq1()), SnjjqdsybgBsInfo::getShiyrq1, bo.getShiyrq1());
        lqw.eq(StringUtils.isNotBlank(bo.getQil1()), SnjjqdsybgBsInfo::getQil1, bo.getQil1());
        lqw.eq(StringUtils.isNotBlank(bo.getKangzqdjszb1()), SnjjqdsybgBsInfo::getKangzqdjszb1, bo.getKangzqdjszb1());
        lqw.eq(StringUtils.isNotBlank(bo.getKangzqddz11()), SnjjqdsybgBsInfo::getKangzqddz11, bo.getKangzqddz11());
        lqw.eq(StringUtils.isNotBlank(bo.getKangzqddz12()), SnjjqdsybgBsInfo::getKangzqddz12, bo.getKangzqddz12());
        lqw.eq(StringUtils.isNotBlank(bo.getKangzqddz13()), SnjjqdsybgBsInfo::getKangzqddz13, bo.getKangzqddz13());
        lqw.eq(StringUtils.isNotBlank(bo.getKangzqdpjz1()), SnjjqdsybgBsInfo::getKangzqdpjz1, bo.getKangzqdpjz1());
        lqw.eq(StringUtils.isNotBlank(bo.getKangzqdjgpd1()), SnjjqdsybgBsInfo::getKangzqdjgpd1, bo.getKangzqdjgpd1());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqdjszb1()), SnjjqdsybgBsInfo::getKangyqdjszb1, bo.getKangyqdjszb1());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqddz11()), SnjjqdsybgBsInfo::getKangyqddz11, bo.getKangyqddz11());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqddz12()), SnjjqdsybgBsInfo::getKangyqddz12, bo.getKangyqddz12());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqddz13()), SnjjqdsybgBsInfo::getKangyqddz13, bo.getKangyqddz13());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqddz14()), SnjjqdsybgBsInfo::getKangyqddz14, bo.getKangyqddz14());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqddz15()), SnjjqdsybgBsInfo::getKangyqddz15, bo.getKangyqddz15());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqddz16()), SnjjqdsybgBsInfo::getKangyqddz16, bo.getKangyqddz16());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqdpjz1()), SnjjqdsybgBsInfo::getKangyqdpjz1, bo.getKangyqdpjz1());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqdjgpd1()), SnjjqdsybgBsInfo::getKangyqdjgpd1, bo.getKangyqdjgpd1());
        lqw.eq(StringUtils.isNotBlank(bo.getQvybw2()), SnjjqdsybgBsInfo::getQvybw2, bo.getQvybw2());
        lqw.eq(StringUtils.isNotBlank(bo.getZijrq2()), SnjjqdsybgBsInfo::getZijrq2, bo.getZijrq2());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyrq2()), SnjjqdsybgBsInfo::getShiyrq2, bo.getShiyrq2());
        lqw.eq(StringUtils.isNotBlank(bo.getQil2()), SnjjqdsybgBsInfo::getQil2, bo.getQil2());
        lqw.eq(StringUtils.isNotBlank(bo.getKangzqdjszb2()), SnjjqdsybgBsInfo::getKangzqdjszb2, bo.getKangzqdjszb2());
        lqw.eq(StringUtils.isNotBlank(bo.getKangzqddz21()), SnjjqdsybgBsInfo::getKangzqddz21, bo.getKangzqddz21());
        lqw.eq(StringUtils.isNotBlank(bo.getKangzqddz22()), SnjjqdsybgBsInfo::getKangzqddz22, bo.getKangzqddz22());
        lqw.eq(StringUtils.isNotBlank(bo.getKangzqddz23()), SnjjqdsybgBsInfo::getKangzqddz23, bo.getKangzqddz23());
        lqw.eq(StringUtils.isNotBlank(bo.getKangzqdpjz2()), SnjjqdsybgBsInfo::getKangzqdpjz2, bo.getKangzqdpjz2());
        lqw.eq(StringUtils.isNotBlank(bo.getKangzqdjgpd2()), SnjjqdsybgBsInfo::getKangzqdjgpd2, bo.getKangzqdjgpd2());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqdjszb2()), SnjjqdsybgBsInfo::getKangyqdjszb2, bo.getKangyqdjszb2());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqddz21()), SnjjqdsybgBsInfo::getKangyqddz21, bo.getKangyqddz21());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqddz22()), SnjjqdsybgBsInfo::getKangyqddz22, bo.getKangyqddz22());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqddz23()), SnjjqdsybgBsInfo::getKangyqddz23, bo.getKangyqddz23());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqddz24()), SnjjqdsybgBsInfo::getKangyqddz24, bo.getKangyqddz24());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqddz25()), SnjjqdsybgBsInfo::getKangyqddz25, bo.getKangyqddz25());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqddz26()), SnjjqdsybgBsInfo::getKangyqddz26, bo.getKangyqddz26());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqdpjz2()), SnjjqdsybgBsInfo::getKangyqdpjz2, bo.getKangyqdpjz2());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqdjgpd2()), SnjjqdsybgBsInfo::getKangyqdjgpd2, bo.getKangyqdjgpd2());
        lqw.eq(StringUtils.isNotBlank(bo.getQvybw3()), SnjjqdsybgBsInfo::getQvybw3, bo.getQvybw3());
        lqw.eq(StringUtils.isNotBlank(bo.getZijrq3()), SnjjqdsybgBsInfo::getZijrq3, bo.getZijrq3());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyrq3()), SnjjqdsybgBsInfo::getShiyrq3, bo.getShiyrq3());
        lqw.eq(StringUtils.isNotBlank(bo.getQil3()), SnjjqdsybgBsInfo::getQil3, bo.getQil3());
        lqw.eq(StringUtils.isNotBlank(bo.getKangzqdjszb3()), SnjjqdsybgBsInfo::getKangzqdjszb3, bo.getKangzqdjszb3());
        lqw.eq(StringUtils.isNotBlank(bo.getKangzqddz31()), SnjjqdsybgBsInfo::getKangzqddz31, bo.getKangzqddz31());
        lqw.eq(StringUtils.isNotBlank(bo.getKangzqddz32()), SnjjqdsybgBsInfo::getKangzqddz32, bo.getKangzqddz32());
        lqw.eq(StringUtils.isNotBlank(bo.getKangzqddz33()), SnjjqdsybgBsInfo::getKangzqddz33, bo.getKangzqddz33());
        lqw.eq(StringUtils.isNotBlank(bo.getKangzqdpjz3()), SnjjqdsybgBsInfo::getKangzqdpjz3, bo.getKangzqdpjz3());
        lqw.eq(StringUtils.isNotBlank(bo.getKangzqdjgpd3()), SnjjqdsybgBsInfo::getKangzqdjgpd3, bo.getKangzqdjgpd3());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqdjszb3()), SnjjqdsybgBsInfo::getKangyqdjszb3, bo.getKangyqdjszb3());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqddz31()), SnjjqdsybgBsInfo::getKangyqddz31, bo.getKangyqddz31());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqddz32()), SnjjqdsybgBsInfo::getKangyqddz32, bo.getKangyqddz32());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqddz33()), SnjjqdsybgBsInfo::getKangyqddz33, bo.getKangyqddz33());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqddz34()), SnjjqdsybgBsInfo::getKangyqddz34, bo.getKangyqddz34());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqddz35()), SnjjqdsybgBsInfo::getKangyqddz35, bo.getKangyqddz35());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqddz36()), SnjjqdsybgBsInfo::getKangyqddz36, bo.getKangyqddz36());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqdpjz3()), SnjjqdsybgBsInfo::getKangyqdpjz3, bo.getKangyqdpjz3());
        lqw.eq(StringUtils.isNotBlank(bo.getKangyqdjgpd3()), SnjjqdsybgBsInfo::getKangyqdjgpd3, bo.getKangyqdjgpd3());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), SnjjqdsybgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), SnjjqdsybgBsInfo::getFujsm, bo.getFujsm());
        return lqw;
    }

    /**
     * 新增水泥净浆强度试验报告
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(SnjjqdsybgBsInfoBo bo) {
        SnjjqdsybgBsInfo add = BeanUtil.toBean(bo, SnjjqdsybgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 多个keys
            ArrayList<TgsybgBaDetail> details = new ArrayList<>(6);
            for (Long key : keys) {
                details.add(new TgsybgBaDetail(add.getExcelId(), key));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改水泥净浆强度试验报告
     */
    @Override
    @DSTransactional
    @ComputeReportResult
    public Boolean updateByBo(SnjjqdsybgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        SnjjqdsybgBsInfo update = BeanUtil.toBean(bo, SnjjqdsybgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos().values(), TgsybgBaDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(SnjjqdsybgBsInfoBo bo) {
        if ("不符合".equals(bo.getKangzqdjgpd1())) {
            bo.setExcelResult(2);
            return;
        }
        if ("不符合".equals(bo.getKangzqdjgpd2())) {
            bo.setExcelResult(2);
            return;
        }
        if ("不符合".equals(bo.getKangzqdjgpd3())) {
            bo.setExcelResult(2);
            return;
        }
        if ("不符合".equals(bo.getKangyqdjgpd1())) {
            bo.setExcelResult(2);
            return;
        }
        if ("不符合".equals(bo.getKangyqdjgpd2())) {
            bo.setExcelResult(2);
            return;
        }
        if ("不符合".equals(bo.getKangyqdjgpd3())) {
            bo.setExcelResult(2);
            return;
        }
        bo.setExcelResult(1);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SnjjqdsybgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水泥净浆强度试验报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public SnjjqdsybgBsInfoVo jcjl(SnjjqdsybgBsInfoVo vo) {

        StringBuilder result = new StringBuilder("检测结论：经检测，该水泥净浆试件抗折抗压强度符合JTG/T 3650-2020《公路桥涵施工技术规范》及设计文件要求。");
        vo.setJiancjl(result.toString());
        return vo;
    }
}
